#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <sys/types.h>

#define NUM 2000

int num = 0;

void* trd1(void* arg) {
    int n;
    for(int i = 0; i < NUM; i++) {
        n = num;
        n++;
        num = n;

        printf("线程1: [%d]\n", num);
    }
}

void* trd2(void* arg) {
    int n;
    for(int i = 0; i < NUM; i++) {
        n = num;
        n++;
        num = n;

        printf("线程2: [%d]\n", num);
    }
}

int main()
{
    pthread_t tid1;
    int ret1 = pthread_create(&tid1, NULL, trd1, NULL);
    if(ret1 != 0) {
        printf("1: pthread_create error %s\n", strerror(ret1));
        exit(1);
    }


    pthread_t tid2;
    int ret2 = pthread_create(&tid1, NULL, trd2, NULL);
    if(ret2 != 0) {
        printf("2: pthread_create error %s\n", strerror(ret2));
        exit(1);
    }

    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    
    printf("num = [%d]\n", num);

    sleep(1);

    return 0;
}

